package com.example.classcommunity3.service;

import com.example.classcommunity3.dao.StudentDao;
import com.example.classcommunity3.dao.TeacherDao;
import com.example.classcommunity3.dao.UserDao;
import com.example.classcommunity3.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    TeacherService teacherService;

    @Autowired
    StudentService studentService;

    @Autowired
    AdminService adminService;

    //用户登录
    public HashMap<String, Object> login(Long account, String psw, String character) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("account", account);

        //管理员登录
        if (character.equals("admin")) {
            return adminService.login(account, psw);
        }
        //用户登录
        else {
            User user = userDao.findByUAccount(account);
            //登录失败
            if (user == null || !psw.equals(user.getuPassword())) {
                hashMap.put("verify", false);
            }
            //登录成功
            else {
                hashMap.put("verify", true);
                try {
                    String name = studentService.getStuName(account);
                    hashMap.put("name", name);
                    hashMap.put("character", "student");
                } catch (Exception e) {
                    String name = teacherService.getTName(account);
                    hashMap.put("name", name);
                    hashMap.put("character", "teacher");
                }
            }
        }
        return hashMap;
    }

    //用户注册
    public boolean signIn(Long account, String psw, String character, String name) {

        boolean b;

        User user = userDao.findByUAccount(account);
        if (user == null) {
            //先设置User表
            User insertUser = new User();
            insertUser.setuAccount(account);
            insertUser.setuCharacter(character);
            insertUser.setuPassword(psw);
            userDao.save(insertUser);
            b = true;
            //在添加student或者teacher表数据
            switch (character) {
                case "teacher":
                    b = teacherService.signIn(account, name);
                    break;
                case "student":
                    b = studentService.signIn(account, name);
                    break;
            }
        } else {
            b = false;
        }
        return b;
    }

    //修改密码
    public boolean changePsw(Long account, String pswOld, String pswNew) {

        User user = userDao.findByUAccountAndUPassword(account, pswOld);

        if (user != null) {
            user.setuPassword(pswNew);
            userDao.save(user);
            return true;
        }
        return false;
    }

    //修改个人信息
    public boolean changeInfo(Long account, String character, String name) {

        switch (character) {
            case "teacher":
                return teacherService.changeInfo(account, name);
            case "student":
                return studentService.changeInfo(account, name);
        }
        return false;
    }
}
